package org.telegram.bot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.enums.StatusEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.telegram.bot.domain.BotConfigWord;
import org.telegram.bot.mapper.BotConfigWordMapper;
import org.telegram.bot.mq.producer.GroupCacheRefreshProducer;
import org.telegram.bot.service.IBotConfigWordService;

import java.util.Arrays;
import java.util.List;

/**
 * 违禁词触发Service业务层处理
 *
 * @author wyx
 * @date 2024-11-23
 */
@Slf4j
@Service
public class BotConfigWordServiceImpl implements IBotConfigWordService
{
    @Resource
    private BotConfigWordMapper botConfigWordMapper;
    
    @Resource
    private GroupCacheRefreshProducer groupCacheRefreshProducer;
    /**
     * 查询违禁词触发
     *
     * @param id 违禁词触发主键
     * @return 违禁词触发
     */
    @Override
    public BotConfigWord selectBotConfigWordById(Long id)
    {
        BotConfigWord botConfigWord = botConfigWordMapper.selectOne(new LambdaQueryWrapper<BotConfigWord>().eq(BotConfigWord::getChatId,id));
        if(botConfigWord==null){
            botConfigWord = botConfigWordMapper.selectById(id);
        }
        return botConfigWord;
    }

    /**
     * 查询违禁词触发列表
     *
     * @param botConfigWord 违禁词触发
     * @return 违禁词触发
     */
    @Override
    public List<BotConfigWord> selectBotConfigWordList(BotConfigWord botConfigWord)
    {
        return botConfigWordMapper.selectBotConfigWordList(botConfigWord);
    }

    /**
     * 新增违禁词触发
     *
     * @param botConfigWord 违禁词触发
     * @return 结果
     */
    @Override
    public int insertBotConfigWord(BotConfigWord botConfigWord)
    {
        botConfigWordMapper.insert(botConfigWord);
        return groupCacheRefreshProducer.sendMessage(botConfigWord.getChatId());
    }

    /**
     * 修改违禁词触发
     *
     * @param botConfigWord 违禁词触发
     * @return 结果
     */
    @Override
    public int updateBotConfigWord(BotConfigWord botConfigWord)
    {
        if(botConfigWord.getId() ==null && botConfigWord.getChatId()!=null){
            BotConfigWord bean = botConfigWordMapper.selectOne(new LambdaQueryWrapper<BotConfigWord>().eq(BotConfigWord::getChatId, botConfigWord.getChatId()));
            if(bean==null){
                return botConfigWordMapper.insert(botConfigWord);
            }
            else{
                botConfigWord.setId(bean.getId());
            }
        }
        botConfigWordMapper.updateById(botConfigWord);
        return groupCacheRefreshProducer.sendMessage(botConfigWord.getChatId());
    }

    /**
     * 批量删除违禁词触发
     *
     * @param ids 需要删除的违禁词触发主键
     * @return 结果
     */
    @Override
    public int deleteBotConfigWordByIds(Long[] ids)
    {
        List<Long> idList = Arrays.asList(ids);
        List<BotConfigWord> botConfigWords = botConfigWordMapper.selectList(new LambdaQueryWrapper<BotConfigWord>().select(BotConfigWord::getChatId).in(BotConfigWord::getId, idList));
        List<Long> chatIds = botConfigWords.stream().map(BotConfigWord::getChatId).toList();
        botConfigWordMapper.deleteByIds(Arrays.asList(ids));
        groupCacheRefreshProducer.sendMessage(chatIds);
        return ids.length;
    }

    /**
     * 删除违禁词触发信息
     *
     * @param id 违禁词触发主键
     * @return 结果
     */
    @Override
    public int deleteBotConfigWordById(Long id)
    {
        BotConfigWord botConfigWord = botConfigWordMapper.selectById(id);
        botConfigWordMapper.deleteById(id);
        return groupCacheRefreshProducer.sendMessage(botConfigWord.getChatId());
    }
    
    /**
     * 默认开启违禁词配置
     * @param chatId
     */
    @Override
    public void init(Long chatId) {
        BotConfigWord botConfigWord = botConfigWordMapper.selectOne(new LambdaQueryWrapper<BotConfigWord>().eq(BotConfigWord::getChatId,chatId));
        if(botConfigWord==null){
            botConfigWord = new BotConfigWord();
            botConfigWord.setChatId(chatId);
            botConfigWord.setStatus(StatusEnum.OK.getCode());
            botConfigWord.setBanNickName(StatusEnum.OK.getCode());
            botConfigWord.setBanTelegramUrl(StatusEnum.OK.getCode());
            botConfigWord.setBanTime(60L);
            insertBotConfigWord(botConfigWord);
        }
    }
}
